package com.imagecollector.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imagecollector.common.R;
import com.imagecollector.common.enums.CodeEnum;
import com.imagecollector.domain.dto.PictureSearchDTO;
import com.imagecollector.domain.entity.Picture;
import com.imagecollector.domain.vo.PictureVO;
import com.imagecollector.mapper.PictureMapper;
import com.imagecollector.service.IPictureService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 图片数据表 服务实现类
 * </p>
 *
 * @author wrf
 * @since 2024-07-31
 */
@Service
public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture> implements IPictureService {
    @Resource
    private PictureMapper pictureMapper;
    @Override
    public R pageQueryPictures(PictureSearchDTO requestParam) {
        //页号
        Integer pageNo = requestParam.getPageNo();
        //页大小
        Integer pageSize = requestParam.getPageSize();
        if (pageNo == null || pageSize == null || pageNo < 1 || pageSize < 1) {
            return R.fail(CodeEnum.FAIL.getCode(), "页数和每页数量有误");
        }
        //开始查找的位置
        pageNo = (pageNo - 1) * pageSize;
        requestParam.setPageNo(pageNo);
        //查询图片--通过类型转换器将json转成List<String>
        List<PictureVO> pictureVOS = pictureMapper.pageQueryPictures(requestParam);
        if (pictureVOS != null) {
            HashMap<String, Object> result = new HashMap<>();
            result.put("pictures", pictureVOS);
            //获取总数
            result.put("total", pictureMapper.countPageQueryPictures(requestParam));
            return R.success().data("data", result);
        }
        return R.fail();
    }

        @Override
        public Picture getPicture (Integer taskId, Integer userId, Integer submitRecordId){
            return lambdaQuery()
                    .eq(Picture::getTaskId, taskId)
                    .eq(Picture::getUserId, userId)
                    .eq(Picture::getSubmitRecordId, submitRecordId).one();
        }

}
